package com.zhb.cloud.mybatis.sys.service.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.querydsl.core.types.Predicate;
import com.zhb.cloud.constants.SessionConstants;
import com.zhb.cloud.mybatis.sys.model.User;
import static com.zhb.cloud.mybatis.sys.model.QUser.user;

import javax.transaction.Transactional;

import com.zhb.cloud.mybatis.sys.repository.UserRepository;
import com.zhb.cloud.mybatis.sys.service.UserService;
import com.zhb.cloud.redis.service.RedisService;

/**
 * @Title: UserServiceImpl.java
 * @Package com.zhb.mybatis.sys.service
 * @Description: 用户管理
 * @author zhb
 * @date 2016年8月3日 下午3:24:46
 * @version V1.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	private RedisService redisService;
	@Autowired
	UserRepository userRepository;

	@Override
	public User findById(String id) {
		return userRepository.findOne(id);
	}

	@Override
	public User saveModel(User model) {
		return userRepository.save(model);
	}

	@Override
	public User updateModel(User model) {
		return userRepository.save(model);

	}

	@Override
	public void deleteModel(String id) {
		userRepository.delete(id);
	}

	@Override
	public long totalAmount(User model, Object... objects) {
		return userRepository.count();
	}

	@Override
	public Page<User> selectModelByCondition(int pageNum, int pageSize) {
		return userRepository.findAll(new PageRequest(pageNum, pageSize));
	}

	@Override
	public User selectByPhone(String phone) {
		return userRepository.findByPhone(phone);
	}

	@Override
	public void updatePasswordByPhone(String encryptionPassword, String userName) {
		Predicate predicate = user.userName.eq(userName);
		User findOne = userRepository.findOne(predicate);
		findOne.setPassword(encryptionPassword);
		userRepository.save(findOne);

	}

	@Override
	public User findByUserName(String username) {
		return userRepository.findByUserName(username);
	}

	/**
	 * 用户登录记录到redis
	 * 
	 * @param username
	 * @param password
	 * @throws JsonProcessingException
	 */
	public String login(String username, String password) {
		// 生成ticket
		String ticket = DigestUtils.md5Hex(System.currentTimeMillis() + username);
		// 保存用户信息到redis中
		this.redisService.set(SessionConstants.USER_TICKET + ticket, username, 60 * 30);
		// 登录成功，返回ticket
		return ticket;
	}

	/**
	 * 根据ticket查询用户数据
	 * 
	 * @param ticket
	 * @return
	 */
	public User queryUserByTicket(String ticket) {
		try {
			// 从redis中查询用户信息
			String jsonData = this.redisService.get(SessionConstants.USER_TICKET + ticket);
			this.redisService.expire(SessionConstants.USER_TICKET + ticket, 60 * 60);
			if (StringUtils.isNotBlank(jsonData)) {
				// 反序列为user对象
				User user = JSONObject.parseObject(jsonData, User.class);
				return user;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
